class TopicsController < ApplicationController
  before_action :set_topic, only: [:show, :edit, :update, :destroy, :follow, :unfollow, :pay]
  before_action :set_articles, only: [:show]
  before_action :authenticate_user!, except: [:index, :show, :search, :search_result]

  # GET /topics
  # GET /topics.json
  def index
    @articles = Article.page get_page
  end

  # GET /topics/1
  # GET /topics/1.json
  def show
    @title = @topic.title
  end

  def follow
    if current_user.id == @topic.user_id
      flash[:notice] = '不能关注自己'
    else
      set_follow_topic_ship
      flash[:notice] = '关注成功'
    end
    redirect_to @topic
  end

  def unfollow
    @title = @topic.title
    if current_user.follow_topics.include?(@topic)
      get_follow_topic_ship.destroy
    else
    end
    redirect_to @topic
  end


  def search
    @title = keywords = params[:keywords]
    if keywords == ""
      redirect_to request.referrer, notice: '请输入要搜索的内容'
    else
      redirect_to search_result_path(keywords)
    end
  end

  def search_result
    @title = @keywords = params[:keywords]

    @search = Topic.search do
      fulltext params[:keywords] do
        fields(:title, :intro)
      end
      paginate page: get_page
    end
  end

  # GET /topics/new
  def new
    @title = '发布主题'
    @topic = Topic.new
  end

  # GET /topics/1/edit
  def edit
    @title = '编辑主题'
  end

  # POST /topics
  # POST /topics.json
  def create
    @topic = Topic.new(topic_params)
    @topic.user_id = current_user.id
    @topic.tags = get_topic_tags
    respond_to do |format|
      if @topic.save
        format.html { redirect_to @topic, notice: '主题已保存.' }
        format.json { render :show, status: :created, location: @topic }
      else
        format.html { render :new }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /topics/1
  # PATCH/PUT /topics/1.json
  def update
    respond_to do |format|
      @topic.tags = get_topic_tags
      if @topic.update(topic_params)
        format.html { redirect_to @topic, notice: '主题已保存。' }
        format.json { render :show, status: :ok, location: @topic }
      else
        format.html { render :edit }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /topics/1
  # DELETE /topics/1.json
  def destroy
    if @topic.articles.size > 0
      redirect_to topic_path(@topic), notice: '不能删除非空主题。'
    else
      @topic.update(is_deleted: true)
      respond_to do |format|
        format.html { redirect_to topics_url, notice: '主题已删除.' }
        format.json { head :no_content }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_topic
      @topic = Topic.find(params[:id])
    end

    def set_articles
      @articles = @topic.articles.page get_page
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def topic_params
      params[:topic].permit(:title, :intro, :tag_ids, :score)
    end

    def get_topic_tags
      # 注意，每次POST过来的数据都有一个空字符
      # ids: {'',1}
      tag_ids = params[:topic][:tag_ids].delete_if{|tag_id| tag_id==''}
      # 更新tag.count和tag_topic_ship
      TagTopicShip.where(topic_id: @topic.id).each do |ship|
        Tag.find(ship.tag_id).count = Tag.find(ship.tag_id).count - 1
        ship.destroy
      end
      Tag.find(tag_ids)
    end

    def get_follow_topic_ship
      FollowTopicShip.where(topic_id: @topic.id, user_id: current_user.id).first
    end

    def set_follow_topic_ship
      FollowTopicShip.create(topic_id: @topic.id, user_id: current_user.id)
    end


end
